import pygame
import sys

# 初始化
pygame.init()
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)

PADDLE_WIDTH, PADDLE_HEIGHT = 100, 20
BALL_DIAMETER = 20
BRICK_WIDTH, BRICK_HEIGHT = 75, 25
BRICK_ROWS = 5
BRICK_COLS = SCREEN_WIDTH // BRICK_WIDTH

FONT_PATH = "C:/Windows/Fonts/msyh.ttc"  # 或 simhei.ttf
FONT = pygame.font.Font(FONT_PATH, 36)

def draw_text(screen, text, size, color, x, y):
    font = pygame.font.Font(FONT_PATH, size)
    surface = font.render(text, True, color)
    rect = surface.get_rect(center=(x, y))
    screen.blit(surface, rect)

def create_bricks():
    bricks = []
    for row in range(BRICK_ROWS):
        for col in range(BRICK_COLS):
            brick_x = col * BRICK_WIDTH
            brick_y = row * BRICK_HEIGHT + 50
            bricks.append(pygame.Rect(brick_x, brick_y, BRICK_WIDTH, BRICK_HEIGHT))
    return bricks

def main():
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('打砖块小游戏')

    clock = pygame.time.Clock()
    game_state = "start"  # start, playing, win, lose

    while True:
        if game_state == "start":
            screen.fill(WHITE)
            draw_text(screen, "打砖块小游戏", 64, BLUE, SCREEN_WIDTH//2, SCREEN_HEIGHT//3)
            draw_text(screen, "按空格键开始", 36, BLACK, SCREEN_WIDTH//2, SCREEN_HEIGHT//2)
            pygame.display.flip()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    game_state = "playing"
                    # 初始化游戏参数
                    paddle_x = (SCREEN_WIDTH - PADDLE_WIDTH) // 2
                    paddle_y = SCREEN_HEIGHT - 50
                    ball_x = SCREEN_WIDTH // 2
                    ball_y = SCREEN_HEIGHT // 2
                    ball_speed_x = 5
                    ball_speed_y = 5
                    bricks = create_bricks()
                    score = 0

        elif game_state == "playing":
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                paddle_x = max(0, paddle_x - 7)
            if keys[pygame.K_RIGHT]:
                paddle_x = min(SCREEN_WIDTH - PADDLE_WIDTH, paddle_x + 7)

            # 小球移动
            ball_x += ball_speed_x
            ball_y += ball_speed_y

            # 碰撞检测
            if ball_x <= 0 or ball_x >= SCREEN_WIDTH - BALL_DIAMETER:
                ball_speed_x = -ball_speed_x
            if ball_y <= 0:
                ball_speed_y = -ball_speed_y

            paddle = pygame.Rect(paddle_x, paddle_y, PADDLE_WIDTH, PADDLE_HEIGHT)
            ball_rect = pygame.Rect(ball_x, ball_y, BALL_DIAMETER, BALL_DIAMETER)
            if paddle.colliderect(ball_rect):
                ball_speed_y = -abs(ball_speed_y)
                # 根据碰撞位置调整小球方向
                offset = (ball_x + BALL_DIAMETER/2) - (paddle_x + PADDLE_WIDTH/2)
                ball_speed_x += int(offset // 20)
                ball_speed_x = max(-8, min(8, ball_speed_x))

            hit_index = ball_rect.collidelist(bricks)
            if hit_index != -1:
                hit_brick = bricks.pop(hit_index)
                score += 1
                # 判断是上下还是左右碰撞
                if abs(ball_rect.bottom - hit_brick.top) < 10 and ball_speed_y > 0:
                    ball_speed_y = -ball_speed_y
                elif abs(ball_rect.top - hit_brick.bottom) < 10 and ball_speed_y < 0:
                    ball_speed_y = -ball_speed_y
                else:
                    ball_speed_x = -ball_speed_x

            # 游戏失败
            if ball_y > SCREEN_HEIGHT:
                game_state = "lose"
            # 游戏胜利
            if not bricks:
                game_state = "win"

            # 绘制
            screen.fill(WHITE)
            pygame.draw.rect(screen, BLUE, paddle)
            pygame.draw.circle(screen, RED, (ball_x + BALL_DIAMETER//2, ball_y + BALL_DIAMETER//2), BALL_DIAMETER//2)
            for brick in bricks:
                pygame.draw.rect(screen, RED, brick)
            score_text = FONT.render(f"得分: {score}", True, BLACK)
            screen.blit(score_text, (10, 10))
            pygame.display.flip()
            clock.tick(60)

        elif game_state in ("win", "lose"):
            screen.fill(WHITE)
            if game_state == "win":
                draw_text(screen, "你赢了！", 64, BLUE, SCREEN_WIDTH//2, SCREEN_HEIGHT//3)
            else:
                draw_text(screen, "游戏失败", 64, RED, SCREEN_WIDTH//2, SCREEN_HEIGHT//3)
            draw_text(screen, "按空格键重新开始，ESC退出", 36, BLACK, SCREEN_WIDTH//2, SCREEN_HEIGHT//2)
            pygame.display.flip()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        game_state = "start"
                    elif event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()

if __name__ == "__main__":
    main()
